package service

import (
	"context"
	"diygw-ui-go/internal/v1/dao"
	"diygw-ui-go/internal/v1/model/admin"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/util/gconv"
	"time"
)

type logOperations struct{}

// 对象实例化
func LogOperations() *logOperations {
	return &logOperations{}
}

// 获取列表不分页
func (s *logOperations) GetList(ctx context.Context, in admin.LogOperationslistInput) (out admin.LogOperationslistOutput, err error) {
	m := dao.LogOperations.Ctx(ctx).OmitEmpty()
	if err = m.OrderDesc("create_time").Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取列表分页
func (s *logOperations) GetListPage(ctx context.Context, in admin.LogOperationslistInput) (out admin.LogOperationslistOutput, err error) {
	m := dao.LogOperations.Ctx(ctx).OmitEmpty()
	if err = m.Page(in.PageNum, in.PageSize).OrderDesc("create_time").Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取详细信息,缓存10小时
func (s *logOperations) GetOne(ctx context.Context, in admin.LogOperationsOneInput) (out admin.LogOperationsOneInput, err error) {
	err = dao.LogOperations.Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour * 10,
		Name:     "operID-" + gconv.String(in.OperId),
		Force:    false,
	}).Where("oper_id", in.OperId).Scan(&out)
	return
}
